<template>
	<view class="personal">
		<TopBar :indexflag="true" :combakflag="true" :lang="false" :bottom="true" :title="$t('AddShop')"
			barstyle="#45bc1b" Color="#fff"></TopBar>
		<view :class="i18nLocale=='ug'?' ug-from input-from':'input-from'">

			<view class="input-item">
				<view class="title">
					{{$t('ShopugName')}}
				</view>
				<u--input :placeholder="$t('ShopugNamePlaceholder')" fontSize="28rpx" autoBlur border="bottom"
					:adjustPosition='false' v-model="form.ug_name" @blur="changeugblur"></u--input>
			</view>
			<view class="input-item">
				<view class="title">
					{{$t('ShopcnName')}}
				</view>
				<u--input :placeholder="$t('ShopcnNamePlaceholder')" fontSize="28rpx" autoBlur border="bottom"
					:adjustPosition='false'v-model="form.cn_name" @blur="changecnblur"></u--input>
			</view>
			<view class="input-item">
				<view class="title">
					{{$t('ShopPrice')}}
				</view>
				<u--input :placeholder="$t('ShopPricePlaceholder')" border="bottom" fontSize="28rpx"
					:adjustPosition='false'></u--input>
			</view>
			<view class="input-item" @click="changepiker">
				<view class="title">
					{{$t('Category')}}
				</view>
				<u-picker :closeOnClickOverlay="true" :show="pickershow" @cancel="pickershow=false"
					@confirm="changeconfrem" :columns="columns" :keyName="i18nLocale=='ug'?'ug_name':'cn_name'"
					:cancelText="$t('Cancel')" :confirmText="$t('Confirm')" @close="pickershow=false"></u-picker>
				<view class="" style="display: flex;">
					<u--input :placeholder="$t('CategoryPlaceholder')" border="bottom" fontSize="28rpx"
						:adjustPosition='false' :value="form.type"></u--input>
					<u-icon name="arrow-left" color="#999" size="24" v-if="i18nLocale=='ug'"></u-icon>
					<u-icon name="arrow-right" color="#999" size="24" v-if="i18nLocale=='cn'"></u-icon>
				</view>
			</view>
			<view class="input-item">
				<view class="title">
					{{$t('ShopImage')}}
				</view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="1" style="margin-top: 20rpx;"></u-upload>
			</view>
		</view>
		<view class="save-button" @click="changesave">{{$t('Save')}}</view>
	</view>
</template>

<script>
	import TopBar from "@/components/TopBar.vue"
	import Contact from "@/components/Contact.vue"
	import {Translation,} from '@/utils/getAddres.js';
	import {getcategory} from '@/api/all.js';
		
	export default {
		components: {
			TopBar,
			Contact
		},
		data() {
			return {
				pickershow: false,
				fileList1: [],
				form: {
					ug_name: '',
					cn_name: '',
					type: ''
				},
				columns: [],

			};
		},
		computed: {
			i18nLocale() {
				return this.$i18n.locale;
			},
		},
		onLoad() {
			this.getCategory();
		},
		methods: {
			changepiker() {
				this.pickershow = true;
			},
			changeconfrem(e) {
				this.pickershow = false;
				if (this.$i18n.locale == 'ug') {
					this.form.type = e.value[0].ug_name;
					let Trantext = Translation('uy', this.form.type, 'zh');
					console.log(Trantext, '+++++')
				} else {
					this.form.type = e.value[0].cn_name;
				}
			},
			async changeugblur() {
				try {
					if (this.form.cn_name == "" && this.form.ug_name != "") {
						console.log(this.form.ug_name,'++++++')
						let Trantext = await Translation('uy', this.form.ug_name, 'zh');
						console.log(Trantext,'+++++++')
						this.form.cn_name = Trantext;
					}
				} catch (error) {
					console.error('Error:', error);
				}
			
			},
			async changecnblur() {
				try {
					if (this.form.ug_name == "" && this.form.cn_name != "") {
						let Trantext = await Translation('zh', this.form.cn_name, 'uy');
						
						this.form.ug_name = Trantext;
					}
				} catch (error) {
					console.error('Error:', error);
				}

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			//获取分类
			getCategory(){
				let that=this;
				getcategory({}).then((res)=>{
					if(res.code==200){
						let data=res.body.data;
						that.columns.push(data)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.personal {
		padding: $uni-spacing-padding-sm;
		box-sizing: border-box;

		.input-from {
			background-color: #fff;
			width: 100%;
			padding: 10rpx 20rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
			border-radius: $uni-border-radius-lg;
			box-shadow: $uni-bg-shadow;

			.input-item {
				width: 100%;
				// display: flex;
				padding: $uni-spacing-padding-sm;
				box-sizing: border-box;

				input {
					font-size: $uni-font-size-base;

				}

				.title {
					color: $uni-color-title;
					font-size: $uni-font-size-base;
				}
			}
		}

		.selecet-addres {
			background-color: #fff;
			width: 100%;
			padding: 20rpx 40rpx;
			box-sizing: border-box;
			margin-top: $uni-spacing-margin-sm;
			border-radius: $uni-border-radius-lg;
			box-shadow: $uni-bg-shadow;
			color: $uni-color-title;
			font-size: $uni-font-size-base;



		}

		.no-address {
			margin-top: 100rpx;

			text {
				padding: 0rpx 20rpx;
			}
		}

		.address {
			// border-bottom: 2rpx solid;
			display: flex;
			margin-bottom: 20rpx;
			background-color: $uni-bg-color-box;
			width: 100%;
			padding: 30rpx 40rpx;
			box-sizing: border-box;
			margin-top: $uni-spacing-margin-sm;
			border-radius: $uni-border-radius-lg;
			box-shadow: $uni-bg-shadow;

			.left {
				width: 70%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				font-size: $uni-font-size-sm1;

				.user {
					display: flex;

					.name {
						color: #15c45b;
						font-weight: bold;
					}

					.pone {
						padding: 0rpx 40rpx;
						box-sizing: border-box;
					}

				}

				.address-title {
					margin-top: 10rpx
				}
			}

			.right {
				width: 30%;
				display: flex;
				align-items: center;
				justify-content: space-around;

				.raio {
					width: 50rpx;
					height: 50rpx;
					border-radius: 100%;
					border: 4rpx solid $uni-border-color;
					display: flex;
					justify-content: center;
					align-items: center;

				}

				.acradio {
					border: 4rpx solid $uni-color-topic;
					background-color: $uni-color-topic;
				}
			}

		}

		.save-button {
			width: 90%;
			margin: 20rpx auto;
			background-color: $uni-color-topic;
			color: #fff;
			font-size: $uni-font-size-lg;
			padding: $uni-spacing-padding-sm 0rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: $uni-border-radius-lg;
			position: fixed;
			bottom: 5%;
			left: 5%;
		}
	}

	.ug-from {
		direction: rtl;

		/deep/ .uni-input-input {
			direction: rtl !important;
			text-align: right;
		}

		/deep/ .uni-input-placeholder {
			text-align: right;
		}
	}
</style>